<template>
	<view>
		<u-navbar back-icon-color="#FFFFFF" title="商品查询" :background="background" title-color="#FFFFFF"></u-navbar>
		<view class="content">
			<view class="u-search-box">
				<u-search shape="round" :clearabled="true" placeholder="输入商品名称" v-model="keyword" @search="doSearch" @custom="doSearch"></u-search>
			</view>
			<view class="old-search-box">
				<view class="title">
					<view>我搜过的</view>
					<view @click="trash()"><u-icon size="30" name="trash-fill"></u-icon></view>
				</view>
				<view class="list">
					<view class="item" v-for="(item, index) in taglist" :key="index">
						<u-tag :text="item" type="success" shape="square" closeable @click="clickTag(item)" @close="tagClick(index)" />
					</view>
				</view>
			</view>
		</view>
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			keyword: '',
			background: {
				//backgroundColor: '#001f3f'

				// 导航栏背景图
				// background: 'url(https://cdn.uviewui.com/uview/swiper/1.jpg) no-repeat',
				// 还可以设置背景图size属性
				// backgroundSize: 'cover',

				// 渐变色
				backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
			},
			taglist: []
		};
	},
	onLoad() {
		try {
			const token = uni.getStorageSync('token');
			console.log(token);
			if (token) {
				this.userInfo = uni.getStorageSync('userInfo');
				if(!this.userInfo){
					//#ifdef APP-PLUS
					uni.reLaunch({
						url: '/pages/login/index'
					});
					//#endif
					//#ifdef MP-WEIXIN
					uni.reLaunch({
						url: '/pages/login/xcxLogin'
					});
					//#endif
				}
				const value = uni.getStorageSync('taglist');
				if (value) {
					this.taglist = value;
				}
			}else{
				//#ifdef APP-PLUS
				uni.reLaunch({
					url: '/pages/login/index'
				});
				//#endif
				//#ifdef MP-WEIXIN
				uni.reLaunch({
					url: '/pages/login/xcxLogin'
				});
				//#endif
			}
		} catch (e) {
			// error
		}
	},
	methods: {
		doSearch(val) {
			let _t = this;
			console.log(val);
			console.log(_t.keyword);
			if (val) {
				if (_t.taglist.length > 9) {
					_t.taglist.splice(0, 1);
				}
				_t.taglist.unshift(val);
				console.log(_t.taglist);
				uni.setStorageSync('taglist', _t.taglist);
				uni.navigateTo({
					url: '/pages/search/result?keyword=' + val
				});
			} else {
				this.$refs.uToast.show({
					title: '搜索内容不能为空',
					type: 'error'
				});
			}
		},
		tagClick(index) {
			let _t = this;
			console.log(_t.taglist);
			_t.taglist.splice(index, 1);
			console.log(_t.taglist);
			uni.setStorageSync('taglist', _t.taglist);
		},
		clickTag(item) {
			this.keyword = item;
			console.log(item);
		},
		trash() {
			this.taglist = [];
			uni.removeStorageSync('taglist');
		}
	}
};
</script>

<style lang="scss" scoped>
.u-search-box {
}
.content {
	margin: 30rpx;
	//background-color: $u-bg-color;
}
,
.old-search-box {
	margin-top: 30rpx;
	.title {
		margin: 0 10rpx;
		padding-bottom: 20rpx;
		display: flex;
		justify-content: space-between;
	}
	.list {
		display: flex;
		flex-wrap: wrap;
		.item {
			margin: 5rpx 10rpx;
			padding: 5rpx;
		}
	}
}
</style>
